package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.web.otherService.ImgService;
import com.sunjob.common.domain.img.SysImg;
import com.sunjob.common.result.Result;
import com.sunjob.common.util.MROUtils;
import com.sunjob.common.util.PageHelper;
import io.swagger.models.Model;
import io.swagger.models.auth.In;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Wangzurong
 * @create 2022-08-11 10:14
 */
@Controller
@RequestMapping("/system/img")
public class SysImgController extends BaseController {
    String prefix = "/system/img/";
    @Autowired
    ImgService imgService;

    /**
     * 查询所有图片信息
     *
     * @return
     */
    @GetMapping("/query")
    @ResponseBody
    @RequiresPermissions("system:img:query")
    public TableDataInfo queryAllImg(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        Result<List<SysImg>> allImg = imgService.getAllImg();
        List<SysImg> data = allImg.getData();
        return getDataTable(data);
    }

    /**
     * 跳转图片管理页面
     */
    @GetMapping("/view")
    public String toImg() {
        return prefix + "imgManage";
    }

    @GetMapping("/detail/{imgId}")
    public String detail(ModelMap model, @PathVariable("imgId") Long imgId) {
        Result<SysImg> imgById = imgService.getImgById(imgId);
        SysImg data = imgById.getData();
        model.put("imgDetail", data);
        return prefix + "imgDetail";
    }

    @GetMapping("/queryBy")
    @ResponseBody
    public TableDataInfo queryBy(SysImg sysImg, @RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        DateTimeFormatter formatter = new DateTimeFormatterBuilder()
                .appendPattern("yyyy-MM-dd[['T'HH][:mm][:ss]]")
                .parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
                .parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)
                .parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
                .parseDefaulting(ChronoField.MILLI_OF_SECOND, 0)
                .toFormatter();
        Map<String, Object> map = sysImg.getParams();
        for (String t : map.keySet()) {
            if (!(map.get(t) == null || map.get(t).toString().trim().length() == 0)) {
                LocalDateTime parse = LocalDateTime.parse(map.get(t).toString(), formatter);
                // 转换为时间戳
                map.put(t, parse.toInstant(ZoneOffset.of("+8")).toEpochMilli());
            }
        }
        Result<List<SysImg>> listResult = imgService.pullImgBy(sysImg);
        List<SysImg> data = listResult.getData();
        PageHelper<SysImg> pageHelper = new PageHelper(data);
        List<List<SysImg>> pageInfo = pageHelper.getPageInfo(pageNum, pageSize, 1);
        if(pageInfo.size()!=0) return  getDataTable(pageInfo.get(0),data.size());
        else return getDataTable(new ArrayList<>(),data.size());
    }

    @GetMapping("/add")
    public String addImg() {
        return prefix + "/imgAdd";
    }

    @PostMapping("/addImg")
    @ResponseBody
    public Result add(@RequestParam("img") MultipartFile img, @RequestParam("imgName") String imgName, @RequestParam("imgUrl") String imgUrl) {
        SysImg sysImg = new SysImg();
        // 设置图片字节数组
        try {
            sysImg.setData(img.getBytes());
        } catch (Exception e) {
            return Result.fail("图片上传失败");
        }
        // 设置图片名称
        sysImg.setImgName(imgName);
        // 设置图片地址
        sysImg.setImgUrl(imgUrl);
        Result<SysImg> sysImgResult = imgService.pushImg(sysImg);
        SysImg data = sysImgResult.getData();
        return Result.ok(data, "上传图片成功");
    }
}
